package cn.com.baiwei.service.impl;
import cn.com.baiwei.dto.SupplyDto;
import cn.com.baiwei.mapper.OrderDetailsMapper;
import cn.com.baiwei.mapper.PurchaseOrderMapper;
import cn.com.baiwei.mapper.ReceiptOrderMapper;
import cn.com.baiwei.mapper.StockMapper;
import cn.com.baiwei.pojo.OrderDetails;
import cn.com.baiwei.pojo.PurchaseOrder;
import cn.com.baiwei.pojo.ReceiptOrder;
import cn.com.baiwei.pojo.Stock;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author youngMoney
 * @Description //TODO $
 * @Date $ $
 * @Param $
 * @return $
 **/
@Service("wholeOrderService")
public class WholeOrderServiceImpl {
    @Resource
    private PurchaseOrderMapper purchaseOrderMapper;
    @Resource
    private OrderDetailsMapper orderDetailsMapper;
    @Resource
    private StockMapper stockMapper;
    @Resource
    private ReceiptOrderMapper receiptOrderMapper;

    public Map<String, Object> insert(SupplyDto supplyDto) {
        supplyDto.setOrderId(Long.toString(System.currentTimeMillis()));
        supplyDto.setEnteringTime(new Date());
        supplyDto.setOrderStatus(0);
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(supplyDto,purchaseOrder);
        purchaseOrderMapper.insert(purchaseOrder);
        List<OrderDetails> orderDetailsList = supplyDto.getOrderDetailsList();
        orderDetailsList.forEach(orderDetails -> {
            orderDetails.setPurchaseId(supplyDto.getOrderId());
            orderDetailsMapper.insert(orderDetails);
        });
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        return map;

    }


    public Map<String, Object> submitOrder(List<Integer> purchaseOrderIdList) {

        purchaseOrderMapper.updateByIds(purchaseOrderIdList);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "提交成功");
        return map;
    }

    public Map<String, Object> receipt( ReceiptOrder receiptOrder) {
        if(receiptOrderMapper.selectById("" +receiptOrder.getOrderId())==null){
            receiptOrderMapper.insert(receiptOrder);
            PurchaseOrder purchaseOrder = purchaseOrderMapper.selectByOrderId(""+receiptOrder.getOrderId());
            if(receiptOrder.getAction()==1){
                purchaseOrder.setDeliveryTime(new Date());
                purchaseOrder.setOrderStatus(2);
                purchaseOrderMapper.updateById(purchaseOrder);
                List<OrderDetails> orderDetails = orderDetailsMapper.selectById(""+receiptOrder.getOrderId());
                orderDetails.forEach(orderDetails1 -> {
                        Stock stock = stockMapper.selectById(""+orderDetails1.getOrderId());
                        stock.setNum(orderDetails1.getOrderNumber()+stock.getNum());
                        stockMapper.updateById(stock);
                });
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    public Map<String, Object> selectUnacceptedOrder() {
        List<PurchaseOrder> purchaseOrderList = purchaseOrderMapper.selectUnacceptedOrder();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        map.put("data",purchaseOrderList);
        return map;

    }

    public Map<String, Object> selectWhole(String orderId) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectByOrderId(orderId);
        List<OrderDetails> orderDetails = orderDetailsMapper.selectById(orderId);
        SupplyDto supplyDto = new SupplyDto();
        BeanUtils.copyProperties(purchaseOrder,supplyDto);
        supplyDto.setOrderDetailsList(orderDetails);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data",supplyDto);
        return map;

    }

}
